package com.pucheng.bracelet.ui.adapter

import android.content.Context
import android.content.Intent
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.pucheng.bracelet.app.ext.KEY_WATCH_INFO
import com.pucheng.bracelet.databinding.ItemListWatchBinding
//import com.pucheng.bracelet.ui.activity.watch.WatchEditActivity
import me.hgj.jetpackmvvm.demo.app.ext.isNotNull
import java.io.Serializable

class WatchAdapter(
    private val callback: (position: Int, data: WatchData) -> Unit,
    private val callbackFragment: (position: Int, data: WatchData) -> Unit
) :
    ListAdapter<WatchData, WatchHolder>(object : DiffUtil.ItemCallback<WatchData>() {
        override fun areItemsTheSame(oldItem: WatchData, newItem: WatchData): Boolean =
            oldItem.name == newItem.name

        override fun areContentsTheSame(oldItem: WatchData, newItem: WatchData): Boolean =
            oldItem == newItem

    }) {
    private var datas: MutableList<WatchData>? = null
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WatchHolder {
        return WatchHolder(
            ItemListWatchBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
            , {
                it?.apply {
                    if (datas.isNotNull() && datas!!.size > this) {
                        for ((index, data) in datas!!.withIndex()) {
                            if (index != this) {
                                datas!![index].selected = false
                            }
                        }
                        callback.invoke(this, datas!![this])
                        notifyDataSetChanged()
                    }
                }
            }, {
                it?.apply {
                    callbackFragment.invoke(this, datas!![this])
                }
            })
    }

    override fun onBindViewHolder(holder: WatchHolder, position: Int) {
        val item = getItem(position)
        item?.apply {
            holder.bind(this, position)
        }
    }

    override fun submitList(list: MutableList<WatchData>?) {
        datas = list
        super.submitList(datas)
    }
}

class WatchHolder(
    private val binding: ItemListWatchBinding,
    callback: (position: Int?) -> Unit,
    callbackFragment: (position: Int?) -> Unit
) :
    RecyclerView.ViewHolder(binding.root) {
    private var position: Int? = 0
    private var watchData: WatchData? = null

    init {

        binding.setOnSelectClickListener {
            watchData?.apply {
                if (watchData?.selected!!) {
                    watchData?.selected = !watchData?.selected!!
                    bind(watchData, position)
                    callback.invoke(position)
                }
            }
        }
        binding.setOnEditClickListener {
            watchData?.apply {
                callbackFragment.invoke(position)
            }
        }
    }

    fun bind(watchData: WatchData?, position: Int?) {
        this.position = position
        this.watchData = watchData
        binding.apply {
            watch = watchData
            if (!TextUtils.isEmpty(watch!!.src)) {
                Glide.with(ivWatchIcon.context).load(watch!!.src)
                    .into(ivWatchIcon)
            }else{
                Glide.with(ivWatchIcon.context).load(watch!!.bin.toInt())
                    .into(ivWatchIcon)
            }
            executePendingBindings()
        }
    }

}

data class WatchData(
    val id: Int,
    var selected: Boolean,
    val src: String,
    val bin: String,
    val name: String
) : Serializable
